package com.eati.cm.serv_impl;

import com.eati.cm.serv.dispacher.DispMan;
import com.eati.cm.serv.dispacher.LoginDisp;
import com.eati.cm.utils.CastUtil;
import com.eati.cm.utils.IOUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.eati.cm.auth.CmUserAuthProvider;
import com.eati.cm.auth.LoginOnlineToken;
import com.eati.cm.auth.ThirdpartyAuthCBToken;
import com.eati.cm.excp.ThirdPartyAccountUnboundExcp;
import com.eati.cm.model.Login;
import com.eati.cm.model.ThirdPartyAccount;
import com.eati.cm.model.User;
import com.eati.cm.model.readonly.useless_for_fe.LoginPlatform;
import com.eati.cm.repo.LoginRepo;
import com.eati.cm.repo.UserRepo;
import com.eati.cm.serv.ILoginPlatformServ;
import com.eati.cm.serv.ILoginServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

@Service
public class LoginServ implements ILoginServ, UserDetailsService {
    @Autowired
    LoginRepo loginRp;
    
    @Autowired
    ILoginPlatformServ loginPlatServ;

    @Autowired
    UserServ usrServ;

    @Autowired
    DispMan disp;
    
    @Autowired
    ThirdPartyAccountServ thirdpartyAuthInfoServ;

    @Override
    public String getThirdPartyAccountIdByToken(String token){
        return "TESTID";
    }

    @Override
    public LoginDisp getLoginDispByThirdPartyName(String thirdPartyName){
        return (LoginDisp)disp.getDisp(thirdPartyName + "LoginDisp");
    }

    public String genCredenticals(User usr, Login login) throws JsonProcessingException {
    	return CastUtil.toJson(usr) + CastUtil.toJson(login);
    }
    
	@Override
	@Transactional
    public LoginOnlineToken login(ThirdpartyAuthCBToken token){
    	LoginDisp loginDisp = getLoginDispByThirdPartyName(token.thirdpartyAuthPlatformName);
    	LoginPlatform loginPlat = loginPlatServ.findOneByName(token.thirdpartyAuthPlatformName);
    	String thirdpartyUsrId = loginDisp.getThirdpartyUsrId(token.cbCode);
    	ThirdPartyAccount thirdpartyAcc = thirdpartyAuthInfoServ.findByLoginPlatformAndThirdUsrId(loginPlat, thirdpartyUsrId);
    	
    	if(thirdpartyAcc == null) {
    		throw new ThirdPartyAccountUnboundExcp("Thirdacc: " + CastUtil.toJsonNoExcp(token), null);
    	}
    	
    	Login log = new Login(thirdpartyAcc.usr, token.cbCode, thirdpartyAcc);
        loginRp.save(log);
        thirdpartyAcc.usr.login = log;
        usrServ.save(thirdpartyAcc.usr);
        return new LoginOnlineToken(thirdpartyAcc.usr.id, log);
    }

	@Override
    public String getThirdpartyLoginPlatformName(HttpServletRequest hr){
        String[] url = hr.getServletPath().split("/");
        String thirdPartyName = url[url.length - 1];
        return thirdPartyName;
        
    }

    @Override
    public void logout(User usr){
        loginRp.delete(usr.login);
    }

    

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {        //loadLoginByLoginId
        Login login = usrServ.findById(Long.parseLong(s)).login;
        if(login == null){
            return null;
        }
        return org.springframework.security.core.userdetails.User
                .withUsername(String.valueOf(login.usr.id))
                .build();
    }
}
